/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.Controller;

import admin.BusinessLogic.BusinessLogic;
import admin_gui.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author WEN
 */
public class ClerkController {

    private AdminGUI adminGUI;
    private BusinessLogic businesslogic;
    private JPanel mainPanel;
    Vector clerk;
    boolean[] clerkItem;
    int deleteType = 0;             //to remember the selection type before deleting
    String searchSelection = "";

    public ClerkController(JPanel MainPanel) {
        try {
            //    System.out.println("in clerk controller1");
            businesslogic = new BusinessLogic();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ClerkController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ClerkController.class.getName()).log(Level.SEVERE, null, ex);
        }
        mainPanel = MainPanel;
        clerk = new Vector();
        clerkItem = new boolean[4];
        clerkItem[0] = true;
        for (int i = 1; i < 4; i++) {
            clerkItem[i] = false;
        }
    }

    public void manageClerk(JTable ResultTable, Vector v_head,
            String searchField, boolean[] clerkItem) {
    //    System.out.println("serrchField:" + searchField);
        this.clerkItem = clerkItem;
        if (clerkItem[0] == true || searchField.length() == 0) {
   //         System.out.println("inmanage");
            showClerkTable(ResultTable, v_head);
        } else if (clerkItem[1] == true) {
            showClerkTableByID(searchField.trim(), ResultTable, v_head);
            deleteType = 1;
            searchSelection = searchField.trim();
        } else if (clerkItem[2] == true) {
            showClerkTableByFirstName(searchField.trim(), ResultTable, v_head);
            deleteType = 2;
            searchSelection = searchField.trim();
        } else if (clerkItem[3] == true) {
            showClerkTableByLastName(searchField.trim(), ResultTable, v_head);
            deleteType = 3;
            searchSelection = searchField.trim();
        } else {
            JOptionPane.showMessageDialog(null,
                    "Sorry! Can not find that information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void showEditClerkPanel(String clerkID) {
        for (int i = 0; i < clerk.size(); i++) {
            if (((Vector) clerk.elementAt(i)).get(0).toString().equals(clerkID)) {
                Admin_EditClerk editClerkPane = new Admin_EditClerk(this, (Vector) clerk.elementAt(
                        i));
                editClerkPane.setBounds(1, 1, 762, 613);
                mainPanel.removeAll();
                mainPanel.add(editClerkPane);
                mainPanel.validate();
                mainPanel.repaint();
            } else {
        //        System.out.println("else");
            }
        }
    }

    public void showClerkTable(JTable ResultTable, Vector v_head) {
   //     System.out.println("headddddd" + v_head);
   //     System.out.println("vector:" + clerk);
        clerk = businesslogic.getClerk();
        DefaultTableModel tm = new DefaultTableModel(clerk, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void showClerkTableByID(String UserID, JTable ResultTable,
            Vector v_head) {
   //    System.out.println("vecid:" + clerk);
        int ckid = Integer.parseInt(UserID);
        if (clerk == null) {
            ;
        } else {
            clerk.clear();
        }

        clerk = businesslogic.getClerkInfo(ckid);
        DefaultTableModel tm = new DefaultTableModel(clerk, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void showClerkTableByFirstName(String FirstName, JTable ResultTable,
            Vector v_head) {
        clerk.clear();
        clerk = businesslogic.getClerksByFirstName(FirstName);
        DefaultTableModel tm = new DefaultTableModel(clerk, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void showClerkTableByLastName(String LastName, JTable ResultTable,
            Vector v_head) {
        clerk.clear();
        clerk = businesslogic.getClerksByLastName(LastName);
        DefaultTableModel tm = new DefaultTableModel(clerk, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void deleteClerk(String clerkID, JTable ResultTable, Vector v_head) {

        if (businesslogic.deleteClerk(clerkID)) {
            if (deleteType == 0) {
                showClerkTable(ResultTable, v_head);
            } else if (deleteType == 1) {
                showClerkTableByID(searchSelection, ResultTable, v_head);
            } else if (deleteType == 2) {
                showClerkTableByFirstName(searchSelection, ResultTable,
                        v_head);
            } else {
                showClerkTableByLastName(searchSelection, ResultTable,
                        v_head);
            }
        }
    }

    public void updateClerk(String ckid, String password, String firstName,
            String lastName) {
        if (businesslogic.updateClerk(ckid, password, firstName, lastName)) {
            Admin_SearchClerk searchClerkPane = new Admin_SearchClerk(
                    mainPanel);
            searchClerkPane.setBounds(1, 1, 762, 613);
            mainPanel.removeAll();
            mainPanel.add(searchClerkPane);
            mainPanel.validate();
            mainPanel.repaint();
            System.out.println("yes");
        }
    }

    public String addClerk(String fname, String lname, String pwd) {
        String str = null;
        str = businesslogic.addClerk(fname, lname, pwd);
        return str;
    }

    public void goBackSearchClerk() {
        Admin_SearchClerk searchClerkPane = new Admin_SearchClerk(mainPanel);
        searchClerkPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(searchClerkPane);
        mainPanel.validate();
        mainPanel.repaint();
    }
}
